package com.example.platformsharedcommon.util;

import java.io.Serializable;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.platformsharedcommon.exception.RRException;

/**
 * 
 * 数据返回格式
 * @param <T>
 * 
 * @Version: 2.0
 * @Author: peter朱硕磊
 * @Email: 455474281@qq.com
 *
 */
public class ResponseResult implements Serializable {

    /**
     *Comment for <code>serialVersionUID</code>
     */
    private static final long     serialVersionUID = 1697116717881807048L;

    //-- 创建返回数据格式的静态方法

    private static ResponseResult responseResult   = new ResponseResult();

    /**
     * 成功返回
     * @return
     */
    public static ResponseResult ok() {
        return responseResult.success(null);
    }

    /**
     * 成功返回数据
     * @param data
     * @return
     */
    public static ResponseResult ok(Object data) {
        return responseResult.success(data);
    }

    /**
     * 失败返回
     * @return
     */
    public static ResponseResult error() {
        return responseResult.fail();
    }

    /**
     * 失败返回异常消息
     * @param message
     * @return
     */
    public static ResponseResult error(String message) {
        return responseResult.fail(message);
    }

    /**
     * 失败返回异常消息
     * @param message
     * @param status
     * @return
     */
    public static ResponseResult error(String message, Integer status) {
        return responseResult.fail(message, status);
    }

    //-- 在返回的接口获取到的对象能做的一些操作

    /**
     * 获取指定类的对象
     * 先判断状态是否为success, 否则抛出异常
     * 然后转换, 失败也抛出异常
     * @param t
     * @return
     */
    public <T> T getObj(Class<T> t) {
        assertSuccess();
        String dataJson = JSONObject.toJSONString(data);
        return JSONObject.parseObject(dataJson, t);
    }

    /**
     * 获取指定类的对象集合List
     * @param clazz
     * @return
     */
    public <T> List<T> getList(Class<T> clazz) {
        assertSuccess();
        if (data != null) {
            String dataListJson = JSONObject.toJSONString(data);
            return JSONObject.parseArray(dataListJson, clazz);
        }
        return null;
    }

    /**
     * 分页时用这个集合list
     * @param clazz
     * @return
     */
    public <T> List<T> getPageList(Class<T> clazz) {
        assertSuccess();
        String dataListJson = JSONObject.toJSONString(data);
        JSONObject object = JSON.parseObject(dataListJson);
        return JSONObject.parseArray(object.getString("list"), clazz);
    }

    /**
     * 获取指定类的对象集合List
     * @param clazz
     * @return
     */
    public Integer getTotal(Object... objects) {
        assertSuccess();
        String dataListJson = JSONObject.toJSONString(getData());
        JSONObject object = JSON.parseObject(dataListJson);
        Integer integer = object.getInteger("total");
        return integer;
    }

    /**
     * 转换成json串
     * @return
     */
    public String toJsonString() {
        return JSONObject.toJSON(this).toString();
    }

    /**
     * 判断结果的状态是否为正常成功的
     * @return
     */
    public boolean isSuccess() {
        return this.getMeta().isSuccess();
    }

    /**
     * 断言成功, 否则抛出异常
     * 接收到对象之后, 先运行一下这个
     */
    public void assertSuccess() {
        if (!isSuccess()) {
            throw new RRException("ResponseData数据状态错误! " + this.getMeta().getMessage());
        }
    }

    //--私有成员与方法

    /**
     * 元数据, 所有的状态封装
     */
    private Meta   meta;
    /**
     *  响应内容, 数据容器
     */
    private Object data;

    private void clear() {
        meta = null;
        data = null;
    }

    private ResponseResult success(Object data) {
        clear();
        this.meta = new Meta(true, "SUCCESS", 1);
        this.data = data;
        return this;
    }

    private ResponseResult fail() {
        clear();
        this.meta = new Meta(false, "ERROR", 0);
        this.data = null;
        return this;
    }

    private ResponseResult fail(String message, int status) {
        clear();
        this.meta = new Meta(false, message, status);
        this.data = null;
        return this;
    }

    /** 业务出错 */
    private ResponseResult fail(String message) {
        return fail(message, 0);
    }

    public Meta getMeta() {
        return meta;
    }

    public Object getData() {
        return data;
    }

    /**
     * Title: 请求元数据
     */
    private class Meta {

        private boolean success;
        private String  message;
        private int     status;

        public Meta() {
            super();
        }

        public Meta(boolean success, String message, int status) {
            this.success = success;
            this.message = message;
            this.status = status;
        }

        public boolean isSuccess() {
            return success;
        }

        @Override
        public String toString() {
            return "Meta [success=" + success + ", message=" + message + ", status=" + status + "]";
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

    }

    @Override
    public String toString() {
        return "ResponseResult [meta=" + meta + ", data=" + data + "]";
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public void setData(Object data) {
        this.data = data;
    }

}
